{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df7cb4c1-a1b8-4e80-ad88-8878008bde89",
   "metadata": {},
   "outputs": [],
   "source": [
    "# My OpenAI Key\n",
    "import os\n",
    "os.environ['OPENAI_API_KEY'] = \"INSERT OPENAI KEY\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7096589b-daaf-440a-b89d-b4956f2db4b2",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Comparing text-davinci-002 vs. text-davinci-003\n",
    "\n",
    "Does text-davinci-003 do better?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8cfbe6f-4c50-4c4f-90f9-03bb91201ef5",
   "metadata": {},
   "source": [
    "#### text-davinci-002"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b48c6d7a-7a38-440b-8ecb-f43f9050ee54",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n",
      "[nltk_data] Downloading package stopwords to\n",
      "[nltk_data]     /Users/jerryliu/nltk_data...\n",
      "[nltk_data]   Package stopwords is already up-to-date!\n"
     ]
    }
   ],
   "source": [
    "from llama_index import GPTKeywordTableIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext\n",
    "from langchain import OpenAI\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "708b323e-d314-4b83-864b-22a1ead60de9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load index\n",
    "llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name=\"text-davinci-002\"))\n",
    "service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)\n",
    "index = GPTKeywordTableIndex.load_from_disk('index_table.json', service_context=service_context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c2de13b-133f-404e-9661-2acafcdf2573",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = index.query(\"What did the author do after his time at Y Combinator?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b27a05df-7c62-49e3-aa8e-f917a67f2bc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>\n",
       "\n",
       "After a few years, the author decided to step away from Y Combinator to focus on other projects, such as painting and writing essays. In 2013, he handed over control of Y Combinator to Sam Altman. The author's mother passed away in 2014, and after taking some time to grieve, he returned to writing essays and working on Lisp. He continued working on Lisp until 2019, when he finally completed the project.\n",
       "\n",
       "In 2015, the author decided to move to England with his family. They originally intended to only stay for a year, but ended up liking it so much that they remained there. The author wrote Bel while living in England. In 2019, he finally finished the project. After completing Bel, the author wrote a number of essays on various topics. In 2020, he started to think about what his next project should be.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ad95ede-b0ef-46e9-b6d7-cd397d54afc3",
   "metadata": {},
   "source": [
    "#### text-davinci-003"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "be70b923-c562-430b-91ce-30b4a052463e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n",
      "[nltk_data] Downloading package stopwords to\n",
      "[nltk_data]     /Users/jerryliu/nltk_data...\n",
      "[nltk_data]   Package stopwords is already up-to-date!\n"
     ]
    }
   ],
   "source": [
    "from llama_index import GPTKeywordTableIndex, SimpleDirectoryReader, LLMPredictor, ServiceContext\n",
    "from langchain import OpenAI\n",
    "from IPython.display import Markdown, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "66ccf04f-0c19-4e5e-8441-837199aaac75",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load index\n",
    "llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name=\"text-davinci-003\"))\n",
    "service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)\n",
    "index = GPTKeywordTableIndex.load_from_disk('index_table.json', service_context=service_context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b610773d-879c-45c2-a503-5fb691e2b7ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = index.query(\"What did the author do after his time at Y Combinator?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "40a4ca0a-2013-44b8-a937-13fa2e9c60db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>\n",
       "\n",
       "After his time at Y Combinator, the author returned to painting and writing essays. He moved to New York City and rented a rent-controlled apartment in Yorkville. He wrote a book on Lisp and did freelance Lisp hacking work to support himself. He also became the de facto studio assistant for Idelle Weber, an early photorealist painter. He eventually had the idea to start a company to put art galleries online, but the idea was unsuccessful. He then had the idea to write software to build online stores, which became the basis for his successful company, Viaweb. After Viaweb was acquired by Yahoo!, the author returned to painting and started writing essays online. He wrote a book of essays, Hackers & Painters, and worked on spam filters. He also bought a building in Cambridge to use as an office. He then had the idea to start Y Combinator, an investment firm that would make a larger number of smaller investments and help founders remain as CEO. He and his partner Jessica Livingston ran Y Combinator and funded a batch of startups twice a year. He also continued to write essays, cook for groups of friends, and explore the concept of invented vs discovered in software. He created the Summer Founders Program, which invited under</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2298afe3-2c27-4da6-9a31-ff3478f07741",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index",
   "language": "python",
   "name": "llama_index"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
